package com.henu.engineer.service.teacher.cl

import com.alibaba.fastjson.JSONObject
import com.henu.engineer.dao.mapper.AuditXmlMapper
import com.henu.engineer.service.FileService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.multipart.MultipartFile
import org.thymeleaf.util.StringUtils


/**
 *
 * @author noexcept
 * @since 2021/8/13 11:16
 */
@Service
class CourseOutlineCLService {

    @Autowired
    lateinit var fileService: FileService

    @Autowired
    lateinit var auditXmlMapper: AuditXmlMapper

    /**
     * 课程大纲上传：    课程大纲本地保存并将其写入到审核表中     uploadTeacherId为当前登录的老师ID  这个应该在后端也能得到 不知道怎样得到
     */

    fun courseOutline(uploadTeacherId:String?,fileUpload: MultipartFile?):String{
        val res=JSONObject()
        if (fileUpload == null||StringUtils.isEmpty(uploadTeacherId)) {
            res["code"]=1
            res["msg"]="文件或登录用户为空"
            return res.toJSONString()
        }
        val m=fileService.saveUploadedFile(fileUpload)
        if(m.first=="FAILED"||StringUtils.isEmpty(m.second)){
            res["code"]=2
            res["msg"]="文件加入文件夹出错或文件路径为空"
            return res.toJSONString()
        }
        if(!auditXmlMapper.addAudit(4,0,uploadTeacherId!!,m.second!!)){
            res["code"]=3
            res["msg"]="审核表加入失败"
            return res.toJSONString()
        }
        res["code"]=0
        res["msg"]="success"
        return res.toJSONString()
    }

    /**
     * 试卷审批表上传
     */

    fun testOutline(uploadTeacherId:String?,fileUpload: MultipartFile?):String{
        val res=JSONObject()
        if (fileUpload == null||StringUtils.isEmpty(uploadTeacherId)) {
            res["code"]=1
            res["msg"]="文件或登录用户为空"
            return res.toJSONString()
        }
        val m=fileService.saveUploadedFile(fileUpload)
        if(m.first=="FAILED"||StringUtils.isEmpty(m.second)){
            res["code"]=2
            res["msg"]="文件加入文件夹出错或文件路径为空"
            return res.toJSONString()
        }
        if(!auditXmlMapper.addAudit(5,0,uploadTeacherId!!,m.second!!)){
            res["code"]=3
            res["msg"]="审核表加入失败"
            return res.toJSONString()
        }
        res["code"]=0
        res["msg"]="success"
        return res.toJSONString()
    }

}